package net.javamine.util.entityUtils.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.javamine.base.BaseJDBCForMySQLDao;
import net.javamine.util.ExceptionUtils;
import net.javamine.util.entityUtils.entity.ColumnEntity;

import org.apache.log4j.Logger;

/**
 * 实体DAO类
 * @author <a href="mailto: java_lht@163.com">Lht</a>
 * @since 2012-11-30
 * @version 1.0
 */
public class EntityUtilDao {
	
	private static Logger log = Logger.getLogger(EntityUtilDao.class);
	
	/**
	 * 获取指定数据库表中的字段信息
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public List<ColumnEntity> queryTableInfo(String tableName) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<ColumnEntity> colEntityList = new ArrayList<ColumnEntity>();
		try {
			connection = BaseJDBCForMySQLDao.getConnection();
			String sql = "SHOW FULL COLUMNS FROM javamine." + tableName;
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ColumnEntity entity = new ColumnEntity();
				entity.setKey(resultSet.getString("key"));
				entity.setField(resultSet.getString("field"));
				entity.setNoll(resultSet.getString("null"));
				entity.setType(resultSet.getString("type"));
				entity.setComment(resultSet.getString("comment"));
				colEntityList.add(entity);
			}
		} catch (Exception e) {
			ExceptionUtils.getExceptionMessage(log, e);
		} finally {
			BaseJDBCForMySQLDao.close(resultSet, preparedStatement, connection);
		}
		return colEntityList;
	}
	
	public static void main(String[] args) {
		try {
			List<ColumnEntity> proList = new EntityUtilDao().queryTableInfo("tb_blog_blogs");
			for (ColumnEntity entity : proList) {
				System.out.println(entity.getKey());
				System.out.println(entity.getField());
				System.out.println(entity.getNoll());
				System.out.println(entity.getType());
				System.out.println(entity.getComment());
				System.out.println();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
